package com.apop.common.util;
import java.util.*;

/**
 * @ClassName: MapUtil
 * @Description: Map工具
 * @author Jiayi Zheng
 * @date 2021年9月22日
 */
public class MapUtil {

    /**
     * 批量处理大小写
     * @param list
     * @param bLower true 转小写 否则大写
     * @return
     */
    public static List<Map> mapListCaseTransfer(List<Map> list,boolean bLower){
        List<Map> result = new ArrayList<>();
        for(Map m: list){
            Map m2;
            if(bLower){
                m2 = keyToLowerCase(m);
            }else{
                m2 = keyToUpperCase(m);
            }
            result.add(m2);
        }
        return result;
    }

    /**
     * 将map的key转换成小写
     * @param orgMap
     * @return
     */
    public static Map<String, Object> keyToLowerCase(Map<String, Object> orgMap) {
        Map<String, Object> resultMap = new HashMap<>();

        if (orgMap == null || orgMap.isEmpty()) {
            return resultMap;
        }

        Set<Map.Entry<String,Object>> entrySet = orgMap.entrySet();
        for (Map.Entry<String, Object> entry : entrySet) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if(value instanceof List){
                value = mapListCaseTransfer((List)value,true);
            }

            resultMap.put(key.toLowerCase(), value);
        }

        return resultMap;
    }

    /**
     * 将map的key转换成大写
     * @param orgMap
     * @return
     */
    public static Map<String, Object> keyToUpperCase(Map<String, Object> orgMap) {
        Map<String, Object> resultMap = new HashMap<>();

        if (orgMap == null || orgMap.isEmpty()) {
            return resultMap;
        }

        Set<Map.Entry<String,Object>> entrySet = orgMap.entrySet();
        for (Map.Entry<String, Object> entry : entrySet) {
            String key = entry.getKey();
            Object value = entry.getValue();
            resultMap.put(key.toUpperCase(), value);
        }

        return resultMap;
    }
}
